package com.zemo.base.common.enums;

import java.util.EnumSet;

/**
 * 异常状态码及通用方法定义。采用16进制
 * <ul>
 *     <li>通用类,0000~ 00FF</li>
 *     <li>个人资产,0100-01FF</li>
 *     <li>微信,0200-02FF</li>
 *     <li>支付宝,0300-03FF</li>
 *     <li>易宝,0400-04FF</li>
 *     <li>银联,0500-05FF</li>
 * </ul>
 *
 * Created by zemochen on 2018/1/2.
 */
public interface ErrorCodeDef<E extends  Enum> {

    public String getCode();

    public String getMsg();

    public Integer getSystemCode();

    /**
     * 需要jdk 1.8
     * @param type
     * @param value
     * @param <E>
     * @return
     */
    public static <E extends Enum<E>> E of(Class<E> type, String value) {
        EnumSet<E> enums = EnumSet.allOf(type);
        for (Enum<E> en : enums) {
            if (!(en instanceof  ErrorCodeDef)) {
                throw new IllegalArgumentException(type + " is not implemented " + ErrorCodeDef.class);
            }
            ErrorCodeDef code = (ErrorCodeDef) en;
            if (code.getCode() == value) {
                return (E) en;
            }
        }
        throw new IllegalArgumentException(String.format("Invalid argument [%s]", value));
    }

}
